<script setup>
import {ref, computed} from 'vue'
import {ElMessage} from 'element-plus'
import {login} from "@/api/auth.js";
import {useRouter} from "vue-router";
import {useUserStore} from "@/stores/user.js";

const router = useRouter()

const userStore = useUserStore()

const username = ref('')
const password = ref('')

const isLoginDisabled = computed(() => {
  return password.value.length < 6 || !password.value
})

const loading = ref(false)

const handleLogin = () => {
  if (!username.value || username.value.length < 3) {
    ElMessage.error('请输入有效的用户名')
    return
  }
  if (!password.value || password.value.length < 6) {
    ElMessage.error('密码长度不能少于6位')
    return
  }

  loading.value = true
  login(username.value, password.value)
    .then((res) => {
      if (res.code === 200) {
        userStore.setUserInfo({
          alumniId: res.data.alumniId,
          username: res.data.username,
          token: res.data.token
        })
        ElMessage.success('登录成功')
        router.push('/dashboard')
      } else {
        ElMessage.error(res.msg || "登录失败")
      }
    })
    .catch((error) => {
      if (error.response?.data?.msg) {
        ElMessage.error(error.response.data.msg)
      } else if (error.message) {
        ElMessage.error(error.message)
      } else {
        ElMessage.error('登录失败，请稍后重试')
      }
    })
    .finally(() => {
      loading.value = false
    })
}

const handleRegister = () => {
  console.log('跳转到注册页面')
  router.push('/register')
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="header">
        <img src="@/assets/logo.svg" alt="Logo" class="logo">
        <h1 class="title">JMU Alumnus</h1>
      </div>

      <el-form class="login-form" @keyup.enter="handleLogin">
        <el-form-item>
          <el-input
            v-model="username"
            placeholder="请输入用户名"
            :clearable="true"
          />
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            @click="handleLogin"
            :disabled="isLoginDisabled"
            :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>

        <el-form-item style="margin-top: 16px; text-align: center">
          <a href="javascript:void(0)" class="register-link" @click.prevent="handleRegister">
            没有账号？
            <span class="register-text">立即注册</span>
          </a>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 50px;
  height: 50px;
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.login-form {
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

:deep(.el-input__inner) {
  height: 40px;
}

.register-link {
  color: #666;
  text-decoration: none;
  font-size: 14px;
}

.register-text {
  color: #409eff;
  cursor: pointer;
  margin-left: 4px;
}

.register-text:hover {
  color: #66b1ff;
  text-decoration: underline;
}
</style>
